//
//  MyWeiBoTests.swift
//  MyWeiBoTests
//
//  Created by mac on 15/3/2.
//  Copyright (c) 2015年 gpr. All rights reserved.
//

import UIKit
import XCTest

class MyWeiBoTests: XCTestCase {
    
    override func setUp() {
        super.setUp()
        // Put setup code here. This method is called before the invocation of each test method in the class.
    }
    
    override func tearDown() {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
        super.tearDown()
    }
    
    func testTime() {
        // Sat Mar 07 12:01:00 +0800 2015
        let created_at = "Sat Mar 07 15:46:00 +0800 2015"
        let createTimeStrig: String?
        let fmt = NSDateFormatter()
        fmt.dateFormat = "EEE MMM dd HH:mm:ss Z yyyy"
        fmt.locale = NSLocale(localeIdentifier: "en_US")
        let createDate = fmt.dateFromString(created_at)
        let calendar = NSCalendar.currentCalendar()
        let unit = NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.MonthCalendarUnit | NSCalendarUnit.DayCalendarUnit | NSCalendarUnit.HourCalendarUnit | NSCalendarUnit.MinuteCalendarUnit | NSCalendarUnit.SecondCalendarUnit
        let componetCreate =  calendar.components(unit, fromDate: createDate!)
        let componetNow = calendar.components(unit, fromDate: NSDate())
        var dateFormat: String?
        if componetCreate.year != componetNow.year {
            dateFormat = "yyyy-MM-dd"
        } else if componetCreate.month != componetNow.month {
            dateFormat = "MM-dd"
        } else if componetCreate.day != componetNow.day {
            dateFormat = "dd-HH:mm:ss"
        } else if componetCreate.hour != componetNow.hour {
            let since = componetNow.hour - componetCreate.hour
            createTimeStrig = "\(since) 小时前"
            println("----------------" + createTimeStrig!)
            return
        }else if componetCreate.minute != componetNow.minute {
            let since = componetNow.minute - componetCreate.minute
            createTimeStrig = "\(since) 分钟前"
            println("----------------" + createTimeStrig!)
            return
        } else {
            createTimeStrig = "刚刚"
            println("----------------" + createTimeStrig!)
            return
        }
        fmt.dateFormat = dateFormat!
        createTimeStrig = fmt.stringFromDate(createDate!)
        println("----------------" + createTimeStrig!)
    }
    
    func testRegex() {
        // 正则表达式 最终要的三个字符 .匹配任意字符, * 至少有一个 ?非贪婪,出现0或者一次
        //  () 括号代表要提出的内容
        let source = "[哈哈]abuc[呵呵]sfsadf" as NSString
        let pattern  = "\\[(.*?)\\]"
        // CaseInsensitive 大小写敏感
        // DotMatchesLineSeparators 不匹配换行符
        let regx = NSRegularExpression(pattern: pattern, options: NSRegularExpressionOptions.CaseInsensitive |
            NSRegularExpressionOptions.DotMatchesLineSeparators, error: nil)
        // 返回的是 NSTextCheckingResult 的数组
        let results = regx!.matchesInString(source as String, options: NSMatchingOptions.allZeros, range: NSMakeRange(0, source.length)) as! [NSTextCheckingResult]
        
        for item in results {
            // 根据正则表达式 匹配出来的字符串
            let range1 = item.rangeAtIndex(0)
            let str1 = source.substringWithRange(NSMakeRange(range1.location, range1.length))
            println("正则表达式匹配出来结果--" + str1)
            let range2 = item.rangeAtIndex(1)
            let str2 = source.substringWithRange(NSMakeRange(range2.location, range2.length))
            println("括号匹配出来的结果--" + str2)
        }
    }
    
}
